﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using WarehouseManagement.BusinessLogic.BC;

namespace WarehouseManagement.Views.Customer
{
    public partial class NewEditCustomer : DevExpress.XtraEditors.XtraForm
    {
        private bool _isEditMode;
        private CustomerBC _supplierBC;

        public delegate void OperationCompleted(bool success, bool isEditMode);
        public OperationCompleted OnOperationCompleted;

        public NewEditCustomer(bool isEditMode = false)
        {
            InitializeComponent();
            _isEditMode = isEditMode;
            _supplierBC = new CustomerBC();
            if (isEditMode)
            {
                this.Text = "Sửa khách hàng";

            }
            else
            {
                this.Text = "Thêm khách hàng";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var errList = ValidateCategory(_isEditMode);
            if (errList.Count() > 0)
            {
                XtraMessageBox.Show(string.Join("\n", errList), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                var dto = new BusinessLogic.DTO.CustomerDTO()
                    {
                        Address = this.Address,
                        BankAccount = this.BankAccount,
                        BankName = this.BankName,
                        ContactPerson = this.ContactPerson,
                        Email = this.Email,
                        Fax = this.Fax,
                        FirstName = this.FirstName,
                        Id = this.Id,
                        LastName = this.LastName,
                        Mobile = this.Mobile,
                        Phone = this.Phone,
                        SkypeAccount = this.SkypeAccount,
                        YahooAccount = this.YahooAccount
                    };
                if (!_isEditMode)
                {
                    _supplierBC.AddCustomer(dto);
                }
                else
                {
                    _supplierBC.UpdateCustomer(dto);
                }
                FireOperationCompleted(true, _isEditMode);
                this.Close();
            }
            catch (Exception)
            {
                FireOperationCompleted(false, _isEditMode);
            }
        }
        private void FireOperationCompleted(bool success, bool isEditMode)
        {
            if (OnOperationCompleted != null)
                OnOperationCompleted(success, isEditMode);
        }

        public string Address
        {
            get { return txtAddress.Text; }
            set { txtAddress.Text = value; }
        }
        //BankAccount = dto.BankAccount,
        public string BankAccount
        {
            get { return txtBankAccount.Text; }
            set { txtBankAccount.Text = value; }
        }
        //BankName = dto.BankName,
        public string BankName
        {
            get { return txtBankName.Text; }
            set { txtBankName.Text = value; }
        }
        //ContactPerson = dto.ContactPerson,
        public string ContactPerson
        {
            get { return txtContactPerson.Text; }
            set { txtContactPerson.Text = value; }
        }
        //Email = dto.Email,
        public string Email
        {
            get { return txtEmail.Text; }
            set { txtEmail.Text = value; }
        }
        //Fax = dto.Fax,
        public string Fax
        {
            get { return txtFax.Text; }
            set { txtFax.Text = value; }
        }
        //FirstName = dto.FirstName,
        public string FirstName
        {
            get { return txtFirstName.Text; }
            set { txtFirstName.Text = value; }
        }


        //LastName = dto.LastName,
        public string LastName
        {
            get { return txtLastName.Text; }
            set { txtLastName.Text = value; }
        }
        //Mobile = dto.Mobile,
        public string Mobile
        {
            get { return txtMobile.Text; }
            set { txtMobile.Text = value; }
        }
        //Phone = dto.Phone,
        public string Phone
        {
            get { return txtPhone.Text; }
            set { txtPhone.Text = value; }
        }
        //SkypeAccount = dto.SkypeAccount,
        public string SkypeAccount
        {
            get { return txtSkype.Text; }
            set { txtSkype.Text = value; }
        }
        //YahooAccount = dto.YahooAccount
        public string YahooAccount
        {
            get { return txtYahoo.Text; }
            set { txtYahoo.Text = value; }
        }

        public int Id { get; set; }

        private IEnumerable<string> ValidateCategory(bool editMode)
        {
            if (string.IsNullOrEmpty(FirstName))
                yield return "Tên khách hàng không được bỏ trống";

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}